package neu.competition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import neu.competition.dto.TeamDTO;
import neu.competition.dto.UserDTO;
import neu.competition.entity.MembershipType;
import neu.competition.entity.Team;
import neu.competition.entity.User;
import neu.competition.mapper.MembershipTypeMapper;
import neu.competition.mapper.TeamMapper;
import neu.competition.mapper.UserMapper;
import neu.competition.service.TeamService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    TeamMapper tm;
    @Autowired
    MembershipTypeMapper mtm;
    @Autowired
    UserMapper um;

    public List<TeamDTO> SelectTeamsByUserID(String id) {
        QueryWrapper<Team> qw = new QueryWrapper();
        qw.eq("captainid", id);
        List<TeamDTO> ts = tm.selectList(qw).stream().map(t -> {
            TeamDTO dto = new TeamDTO();
            BeanUtils.copyProperties(t, dto);
            return dto;
        }).collect(Collectors.toList());
        return ts;
    }

    public List<UserDTO> selectStudentsByTeamID(int tid) {
        QueryWrapper<MembershipType> qw = new QueryWrapper<>();
        qw.eq("teamid", tid);
        List<UserDTO> us = mtm.selectList(qw).stream().map(t -> {
            UserDTO dto = new UserDTO();
            QueryWrapper<User> qwu = new QueryWrapper<>();
            qwu.eq("userid", t.getUserid());
            BeanUtils.copyProperties(um.selectOne(qwu), dto);
            return dto;
        }).collect(Collectors.toList());
        return us;
    }

    @Override
    public void updateById(int tid, String teamName, String teacher, List<String> students) {
        QueryWrapper<MembershipType> qw = new QueryWrapper<>();
        System.out.println(tid);
        for (String id : students) {
            qw.eq("userid", id);
            qw.eq("teamid", tid);
            if (mtm.selectOne(qw) == null) {
                MembershipType m = new MembershipType();
                m.setTeamid(tid);
                m.setUserid(id);
                m.setMemberType(0);
                mtm.insert(m);
            }
            qw.clear();
        }
        List<UserDTO> us = selectStudentsByTeamID(tid);
        List<MembershipType> del = new ArrayList<>();
        QueryWrapper<Team> qwt = new QueryWrapper<>();
        qwt.eq("teamid", tid);
        Team t = tm.selectOne(qwt);
        for (UserDTO u : us) {
            if (!students.contains(u.getUserid()) && !u.getUserid().equals(t.getCaptainid())) {
                MembershipType m = new MembershipType();
                m.setTeamid(tid);
                m.setUserid(u.getUserid());
                del.add(m);
            }
        }
        for (MembershipType m : del) {
            QueryWrapper<MembershipType> qwm = new QueryWrapper<>();
            qwm.eq("teamid", m.getTeamid());
            qwm.eq("userid", m.getUserid());
            mtm.delete(qwm);
        }
        t.setTeamname(teamName);
        t.setAdviserTeacher(teacher);
        t.setTeamid(tid);
        tm.updateById(t);
    }

    public TeamDTO selectTeamById(int tid) {
        QueryWrapper<Team> qw = new QueryWrapper<>();
        qw.eq("teamid", tid);
        TeamDTO dto = new TeamDTO();
        BeanUtils.copyProperties(tm.selectOne(qw), dto);
        return dto;
    }
}

